<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Perform a regular expression search and replace with multibyte support using a callback</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.mb-ereg-match.html">« mb_ereg_match</a></li>
      <li style="float: right;"><a href="function.mb-ereg-replace.html">mb_ereg_replace »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.mbstring.html">多字节字符串 函数</a></li>
    <li>Perform a regular expression search and replace with multibyte support using a callback</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mb-ereg-replace-callback" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mb_ereg_replace_callback</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.4.1, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">mb_ereg_replace_callback</span> &mdash; <span class="dc-title">Perform a regular expression search and replace with multibyte support using a callback
  </span></p>

 </div>
   
 <div class="refsect1 description" id="refsect1-function.mb-ereg-replace-callback-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mb_ereg_replace_callback</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$pattern</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">string</span><span class="type"></span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code>null</code></strong></span></span><br>): <span class="type"><span class="type">string</span>|<span class="type"><span class="type false">false</span></span>|<span class="type">null</span></span></div>

  <p class="para rdfs-comment">
   Scans <code class="parameter">string</code> for matches to
   <code class="parameter">pattern</code>, then replaces the matched text
   with the output of <code class="parameter">callback</code> function.  
  </p>
  <p class="para">
    The behavior of this function is almost identical to <span class="function"><a href="function.mb-ereg-replace.html" class="function">mb_ereg_replace()</a></span>, 
    except for the fact that instead of
    <code class="parameter">replacement</code> parameter, one should specify a
    <code class="parameter">callback</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mb-ereg-replace-callback-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">pattern</code></dt>

     <dd>

      <p class="para">
       The regular expression pattern.
      </p>
      <p class="para">
       Multibyte characters may be used in <code class="parameter">pattern</code>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">callback</code></dt>

     <dd>

      <p class="para">
        A callback that will be called and passed an array of matched elements
        in the  <code class="parameter">subject</code> string. The callback should
       return the replacement string.
      </p>
      <p class="para">
        You&#039;ll often need the <code class="parameter">callback</code> function
        for a <span class="function"><strong>mb_ereg_replace_callback()</strong></span> in just one place.
        In this case you can use an
        <a href="functions.anonymous.html" class="link">anonymous function</a> to
        declare the callback within the call to
        <span class="function"><strong>mb_ereg_replace_callback()</strong></span>. By doing it this way
        you have all information for the call in one place and do not
        clutter the function namespace with a callback function&#039;s name
        not used anywhere else.        
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       The <span class="type">string</span> being checked.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">options</code></dt>

     <dd>

      <p class="para">
       The search option. See <span class="function"><a href="function.mb-regex-set-options.html" class="function">mb_regex_set_options()</a></span> for explanation.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.mb-ereg-replace-callback-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The resultant <span class="type">string</span> on success, or <strong><code>false</code></strong> on error.
   If <code class="parameter">string</code> is not valid for the current encoding, <strong><code>null</code></strong>
   is returned.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.mb-ereg-replace-callback-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        <code class="parameter">options</code> is nullable now.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        The function checks whether <code class="parameter">string</code> is valid for the
        current encoding.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mb-ereg-replace-callback-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2572">
    <p><strong>示例 #1 <span class="function"><strong>mb_ereg_replace_callback()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;this&nbsp;text&nbsp;was&nbsp;used&nbsp;in&nbsp;2002<br />//&nbsp;we&nbsp;want&nbsp;to&nbsp;get&nbsp;this&nbsp;up&nbsp;to&nbsp;date&nbsp;for&nbsp;2003<br /></span><span style="color: #0000BB">$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"April&nbsp;fools&nbsp;day&nbsp;is&nbsp;04/01/2002\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$text</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Last&nbsp;christmas&nbsp;was&nbsp;12/24/2001\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;callback&nbsp;function<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">next_year</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;as&nbsp;usual:&nbsp;$matches[0]&nbsp;is&nbsp;the&nbsp;complete&nbsp;match<br />&nbsp;&nbsp;//&nbsp;$matches[1]&nbsp;the&nbsp;match&nbsp;for&nbsp;the&nbsp;first&nbsp;subpattern<br />&nbsp;&nbsp;//&nbsp;enclosed&nbsp;in&nbsp;'(...)'&nbsp;and&nbsp;so&nbsp;on<br />&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">].(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]+</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br />echo&nbsp;</span><span style="color: #0000BB">mb_ereg_replace_callback</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"(\d{2}/\d{2}/)(\d{4})"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"next_year"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
April fools day is 04/01/2003
Last christmas was 12/24/2002
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2573">
    <p><strong>示例 #2 <span class="function"><strong>mb_ereg_replace_callback()</strong></span> using anonymous function</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;this&nbsp;text&nbsp;was&nbsp;used&nbsp;in&nbsp;2002<br />//&nbsp;we&nbsp;want&nbsp;to&nbsp;get&nbsp;this&nbsp;up&nbsp;to&nbsp;date&nbsp;for&nbsp;2003<br /></span><span style="color: #0000BB">$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"April&nbsp;fools&nbsp;day&nbsp;is&nbsp;04/01/2002\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$text</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Last&nbsp;christmas&nbsp;was&nbsp;12/24/2001\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">mb_ereg_replace_callback</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"(\d{2}/\d{2}/)(\d{4})"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">].(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]+</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.mb-ereg-replace-callback-notes">
  <h3 class="title">注释</h3>

  <blockquote class="note"><p><strong class="note">注意</strong>: <p class="para"><span class="function"><a href="function.mb-regex-encoding.html" class="function">mb_regex_encoding()</a></span>
指定的内部编码或字符编码将会当作此函数用的字符编码。</p></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mb-ereg-replace-callback-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.mb-regex-encoding.html" class="function" rel="rdfs-seeAlso">mb_regex_encoding()</a> - Set/Get character encoding for multibyte regex</span></li>
    <li class="member"><span class="function"><a href="function.mb-ereg-replace.html" class="function" rel="rdfs-seeAlso">mb_ereg_replace()</a> - Replace regular expression with multibyte support</span></li>
    <li class="member"><a href="functions.anonymous.html" class="link">Anonymous functions</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>